<?php
header('Content-Type:text/html; charset=utf-8');
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();

require_once 'Classes/PHPExcel.php';
//price9point9 dataimport
$what = $_GET['what']?$_GET['what']:'dataimport';
$table = $_GET['table']?$_GET['table']:'content';
//$_GET['date']
if (isset($_GET['date'])){
	$date = urldecode($_GET['date']);
}else{
	exit("参数错误请重新操作。");
}
$itemsinfo = array();
//read excel file;
$PHPExcel = new PHPExcel();
$PHPReader = new PHPExcel_Reader_Excel5();
$PHPExcel = $PHPReader->load('../content/'.$what.'/'.$date);
$currentSheet = $PHPExcel->getSheet(0);
$allColumn = $currentSheet->getHighestColumn();
$allRow = $currentSheet->getHighestRow();
for($currentRow = 1; $currentRow<=$allRow; $currentRow++){
	if($currentRow!=1){
		$itemsinfo[$currentRow]['publishDate'] = $currentSheet->getCell('F'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['picture'] = $currentSheet->getCell('C'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['title'] = $currentSheet->getCell('A'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['goodsUrl'] = $currentSheet->getCell('B'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['desc'] = $currentSheet->getCell('G'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['originalPrice'] = $currentSheet->getCell('D'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['currentPrice'] = $currentSheet->getCell('E'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['sort'] = $currentSheet->getCell('H'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['cate'] = $currentSheet->getCell('I'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['saledNum'] = $currentSheet->getCell('J'.$currentRow)->getValue();
		$itemsinfo[$currentRow]['commission'] = $currentSheet->getCell('K'.$currentRow)->getValue();
		if ($what=='price9point9' || $what=='price19point9'){
			$itemsinfo[$currentRow]['startTime'] = $currentSheet->getCell('L'.$currentRow)->getValue();
			$itemsinfo[$currentRow]['endTime'] = $currentSheet->getCell('M'.$currentRow)->getValue();
			$itemsinfo[$currentRow]['goodsID'] = $currentSheet->getCell('N'.$currentRow)->getValue();
		}else{
			$itemsinfo[$currentRow]['goodsID'] = $currentSheet->getCell('L'.$currentRow)->getValue();
		}
	}
	/* for($currentColumn='A'; $currentColumn<=$allColumn; $currentColumn++){
		//$currentColumn.' || '.$currentRow.'<br />';
		$address = $currentColumn.$currentRow;
		echo $currentSheet->getCell($address)->getValue()."<br />";
		if($currentRow!=1){
			//$itemsinfo
		}
	} */
}
//var_dump($itemsinfo);die();
foreach ($itemsinfo as $key=>$item){
	echo ($key-1)."。";
	//if(strtotime($item['publishDate'])>=strtotime(date("Y-m-d"))){
		//$num=goodsidexist($db,$item['publishDate'],$item['goodsID'],$_SESSION['userName']);
		//$noAccess2=goodsForOne($db,$item['goodsID'],$_SESSION['userName']);
		//if ($num<=0 && $noAccess2<=0){
			if ($what=='price9point9' || $what=='price19point9'){
				//$cateid = 0;
				$db->sql="INSERT INTO `{$table}`(publishDate,picture,title,goodsUrl,`desc`,originalPrice,currentPrice,`status`,sort,comefrom,updater,cateID,saledNum,commission,startTime,endTime,goodsID) VALUES('".$item['publishDate']."','".$item['picture']."','".$item['title']."','".$item['goodsUrl']."','".$item['desc']."',".$item['originalPrice'].",".$item['currentPrice'].",0,".$item['sort'].",2,'".$_SESSION['userName']."',".$item['cate'].",".$item['saledNum'].",".$item['commission'].",".strtotime($item['startTime']).",".strtotime($item['endTime']).",'".$item['goodsID']."')";
				$db->doExecute();
			}else{
				$cateid = getCateId($db, $item['cate']);
				$db->sql="INSERT INTO content(publishDate,picture,title,goodsUrl,`desc`,originalPrice,currentPrice,`status`,sort,comefrom,updater,cateID,saledNum,commission,goodsID) VALUES('".$item['publishDate']."','".$item['picture']."','".$item['title']."','".$item['goodsUrl']."','".$item['desc']."',".$item['originalPrice'].",".$item['currentPrice'].",0,".$item['sort'].",2,'".$_SESSION['userName']."',".$cateid.",".$item['saledNum'].",".$item['commission'].",'".$item['goodsID']."')";
				$db->doExecute();
			}
			echo "添加成功";
		/* }else{
			echo ($key-1)."<span style='color:red;'>添加失败。";
			if ($num>0) {
				echo "本月已有其他编辑添加此商品.";
			}
			if ($noAccess2>0) {
				echo "已被其他责任编辑选用该商品。";
			}
			echo "</span>";
		} */
	//}else{
	//	echo "<span style='color:red;'>添加失败。只能添加今天或者之后的商品，不能添加之前的！</span>";
	//}
	echo "<br />";
}
echo "添加完成！！！";

function getCateId($db,$cate){
	$db->sql = "SELECT id FROM category WHERE cate = '".trim($cate)."'";
	return $db->doResultOne();
}
function goodsidexist($db,$date,$goodsid,$updater){
	$num = 0;
	$year = date('Y',strtotime($date));
	$month=date('n',strtotime($date));
	$db->sql = "SELECT count(id) num FROM content WHERE goodsID='{$goodsid}' AND YEAR(publishDate)={$year} AND MONTH(publishDate)={$month} AND updater != '{$updater}' AND isDel=0 UNION ".
			"SELECT count(id) num FROM priceninepointnine WHERE goodsID='{$goodsid}' AND YEAR(publishDate)={$year} AND MONTH(publishDate)={$month} AND updater != '{$updater}' AND isDel=0 ";
	$numlist = $db->doResultList2();
	foreach ($numlist as $i){
		//var_dump($i)
		$num+=$i[num];
	}
	return $num;
}
//商品归属
function goodsForOne($db,$goodsid,$updater){
	$db->sql="SELECT COUNT(c.id) num FROM content c RIGHT JOIN admin a ON c.updater=a.userName AND a.isActive=1 WHERE c.publishDate >= '2013-04-01' AND c.goodsID = '{$goodsid}' AND c.updater != '{$updater}' UNION ".
			 "SELECT COUNT(c.id) num FROM priceninepointnine c RIGHT JOIN admin a ON c.updater=a.userName AND a.isActive=1 WHERE c.publishDate >= '2013-04-01' AND c.goodsID = '{$goodsid}' AND c.updater != '{$updater}'";
	$goodsInfoTongJi2 = $db->doResultList2();
	foreach ($goodsInfoTongJi2 as $i){
		//var_dump($i)
		$num+=$i['num'];
	}
	$noAccess2=0;
	if ($num>0) {
		$noAccess2=1;
	}
	return $noAccess2;
}
